package com.lyp.crm.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {
	 private static final String[] IGNORE_URI = {"addGoods","login.jsp"};
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		System.out.println("������pre");
        //flag���������ж��û��Ƿ��¼��Ĭ��Ϊfalse 
        boolean flag = true; 
        //��ȡ�����·�������ж�(@control��·��,��"/fore/addGoods")
        String servletPath = request.getServletPath();
            //�ж������Ƿ���Ҫ����
        for (String s : IGNORE_URI) {
          if (servletPath.contains(s)) {
            flag = false;
            break;
          }
        }


        //��������
        if (!flag) {
          //1.��ȡsession�е��û� 
            String name = (String)request.getSession().getAttribute("name");
          //2.�ж��û��Ƿ��Ѿ���¼ 
          if(name == null) {
    //��֤��δ��¼����ת����¼����
         response.sendRedirect("login.jsp");
          } else {
            //����û��Ѿ���¼������֤ͨ��������
             System.out.println("AuthorizationInterceptor��������");
             flag = true;
          }
        }
        return flag;    

    }
		

}
